home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / adahconv.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  5KB  |  229 lines

  1. /*
  2.   HELP ENGINE PARA GWADA
  3.  
  4.   Módulo ADAHCONV.C
  5.   Geraçao do arquivo de help GWADA.HLP
  6.  
  7.   Trabalho de conclusao
  8.   Bracharelado em Informática
  9.  
  10.   Ulrich Peters
  11.   Rafael Presotto
  12.   Jerry Dressler
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <string.h>
  19. #include <..\source\gwadahlp.h>
  20. char moinho[5] = "|/─\\";
  21.  
  22. int read_number(FILE *arquivo)
  23. {
  24.  char i=0,nbuf[8]="";
  25.  int ok=1;
  26.  while (ok)
  27.  {
  28.   fread(&nbuf[i],1,1,arquivo);
  29.   if (nbuf[i] == '\n')
  30.    ok = 0;
  31.   else
  32.    i++;
  33.  }
  34.  nbuf[--i] = '\0';
  35.  return(atoi(nbuf));
  36. }
  37.  
  38. void grava_nodos(FILE *original)
  39. {
  40.  FILE *help;
  41.  char buffer[1025], string[18];
  42.  long size, atual;
  43.  int i=0, tam_cab, offset_cab;
  44.  help = fopen("GWADA.HLP","wb");
  45.  puts("\r\nEscrevendo GWADA.HLP...");
  46.  
  47.  /* Grava o cabeçalho do arquivo de help,
  48.     0x1A, que é marcador de final de arquivo,
  49.     e 0x00 para terminar o string corretamente */
  50.  sprintf(string,"%s%c%c",header,0x1A,0x00);
  51.  tam_cab = strlen(string);
  52.  fwrite(string,tam_cab,1,help);
  53.  
  54.  /* Determina o número de nodos da lista */
  55.  nodo_atual = raiz;
  56.  while (nodo_atual != NULL)
  57.  {
  58.   nodo_atual = nodo_atual->prox;
  59.   i++;
  60.  }
  61.  fwrite(&i,2,1,help); /* Grava número de nodos da lista */
  62.  tam_cab += 3;
  63.  
  64.  /* Grava os nodos - já atualizando o offset */
  65.  offset_cab = tam_cab + i * 12; /* cada grupo de dados é
  66.                    formado por 12 bytes */
  67.  nodo_atual = raiz;
  68.  while (nodo_atual != NULL)
  69.  {
  70.   fwrite(&nodo_atual->number,2,1,help);
  71.   fwrite(&nodo_atual->lines,2,1,help);
  72.   fwrite(&nodo_atual->size,4,1,help);
  73.   nodo_atual->offset += offset_cab;    /* somar tamanho do cabeçalho !! */
  74.   fwrite(&nodo_atual->offset,4,1,help);
  75.   nodo_antes = nodo_atual;
  76.   nodo_atual = nodo_atual->prox;
  77.  }
  78.  
  79.  /* Copiar agora o arquivo de help original */
  80.  fseek(original, 0L, SEEK_END);
  81.  size = ftell(original);
  82.  rewind(original);
  83.  atual = 0;
  84.  i = 0;
  85.  while (atual+1024 <= size)
  86.  {
  87.   fread(&buffer,1024,1,original);
  88.   fwrite(&buffer,1024,1,help);
  89.   atual += 1024;
  90.   printf("%c %d%",moinho[i],100-100*(size-atual)/size);
  91.   gotoxy(1,wherey());
  92.   i++;
  93.   if (i>3) i=0;
  94.  }
  95.  i = (int)(size-atual);
  96.  fread(&buffer,i,1,original);
  97.  fwrite(&buffer,i,1,help);
  98.  fclose(help);
  99. }
  100.  
  101. void ler_arquivo(FILE *arquivo)
  102. {
  103.  char caractere, antes;
  104.  char texto[81];
  105.  long size=0;
  106.  int helps=0, lines=0;
  107.  int desvio;
  108.  int count;
  109.  texto[80] = '\0';
  110.  
  111.  fread(&caractere,1,1,arquivo);
  112.  if (caractere != ':')
  113.  {
  114.   puts("Arquivo deve começar com ':'");
  115.   exit(0);
  116.  }
  117.  
  118.  while (! feof(arquivo))
  119.  {
  120.   nodo_atual = malloc(sizeof(struct tree));
  121.   if (nodo_atual == NULL)
  122.   {
  123.    puts("Memória insuficiente.");
  124.    exit(0);
  125.   }
  126.   nodo_atual->number = read_number(arquivo);
  127.   nodo_atual->offset = ftell(arquivo) - 1;
  128.   nodo_atual->prox = NULL;
  129.  
  130.   antes = caractere;
  131.   nodo_atual->lines = 1;
  132.  
  133.   /* procurar pelo comeco do proximo help */
  134.   while (! ((antes == '\n')&&(caractere == ':')) ) /* return = \r\n */
  135.   {
  136.    antes = caractere;
  137.    fread(&caractere,1,1,arquivo);
  138.    if (caractere == '\n') nodo_atual->lines++;
  139.  
  140.    if (caractere == '\\')
  141.    {
  142.     fread(&caractere,1,1,arquivo);
  143.     if (caractere == 'v')
  144.     {
  145.      /* começou um desvio ?*/
  146.      desvio = 1;
  147.      while (desvio)
  148.      {
  149.       fread(&caractere,1,1,arquivo);
  150.       while ((caractere > 47) && (caractere < 58))
  151.        fread(&caractere,1,1,arquivo);
  152.       if (caractere == '.' )
  153.       {
  154.        printf("\nHelp %d tem desvio que termina sem iniciar antes de\n",nodo_atual->number);
  155.        fread(&texto,80,1,arquivo);
  156.        printf("%c... %s ...%c",34,texto,34);
  157.        exit(1);
  158.       }
  159.       while (caractere != '\\')
  160.       {
  161.        if (caractere == '\n')
  162.        {
  163.     printf("\nHelp %d tem desvio nao-terminado no final da linha antes de\n",nodo_atual->number);
  164.     fread(&texto,80,1,arquivo);
  165.     printf("%c... %s ...%c",34,texto,34);
  166.     exit(1);
  167.        }
  168.        fread(&caractere,1,1,arquivo);
  169.       }
  170.       fread(&caractere,1,1,arquivo);
  171.       if (caractere == 'v')
  172.       {
  173.        /* terminou o texto do desvio - buscar número */
  174.        count = 0;
  175.        while ((caractere != '.')&&(count<6))
  176.        {
  177.     fread(&caractere,1,1,arquivo);
  178.     count++;
  179.        }
  180.        if (count == 6)
  181.        {
  182.     printf("\nHelp %d falta '.' no final de help antes de\n",nodo_atual->number);
  183.     fread(&texto,80,1,arquivo);
  184.     printf("%c... %s ...%c",34,texto,34);
  185.     exit(1);
  186.        }
  187.       }
  188.       desvio = 0;
  189.      }
  190.     }
  191.    }
  192.  
  193.    if (feof(arquivo)) break;
  194.   }
  195.   nodo_atual->size = (size_t)(ftell(arquivo) - nodo_atual->offset - 1);
  196.   nodo_atual->lines -= 1;
  197.  
  198.   if (raiz == NULL)
  199.    raiz = nodo_atual;
  200.   else
  201.    nodo_antes->prox = nodo_atual;
  202.   nodo_antes = nodo_atual;
  203.  
  204.   helps++;
  205.   lines += nodo_atual->lines;
  206.   size += nodo_atual->size;
  207.   printf("%d helps, %d linhas, %lu bytes",helps,lines,size);
  208.   gotoxy(1,wherey());
  209.  }
  210. }
  211.  
  212. void main(int argc, char *argv[])
  213. {
  214.  FILE *arq;
  215.  nodo_antes = NULL;
  216.  if ((arq = fopen(argv[1],"rb")) == NULL)
  217.  {
  218.   printf("Arquivo %s nao localizado.",argv[1]);
  219.   exit(0);
  220.  }
  221.  else
  222.  {
  223.   puts("Lendo help...");
  224.   ler_arquivo(arq);
  225.   grava_nodos(arq);
  226.   fclose (arq);
  227.  }
  228. }
  229.